package ui

import (
	"strconv"
	"kumachan/interpreter/runtime/ui/vdom"
)

type eventHandlerRegistry struct {
	nextId    uint64
	getId     map[*vdom.EventHandler] string
	handlers  map[string] *vdom.EventHandler
}
func newEventHandlerRegistry() *eventHandlerRegistry {
	return &eventHandlerRegistry {
		nextId:   100, // lesser numbers are reserved
		getId:    make(map[*vdom.EventHandler] string),
		handlers: make(map[string] *vdom.EventHandler),
	}
}
func (reg *eventHandlerRegistry) lookupEventHandler(id string) (*vdom.EventHandler, bool) {
	var handler, exists = reg.handlers[id]
	return handler, exists
}
func (reg *eventHandlerRegistry) registerEventHandler(handler *vdom.EventHandler) string {
	var id, exists = reg.getId[handler]
	if !(exists) {
		var id_num = reg.nextId
		reg.nextId += 1
		id = strconv.FormatUint(id_num, 10)
		reg.handlers[id] = handler
		reg.getId[handler] = id
	}
	return id
}
func (reg *eventHandlerRegistry) unregisterEventHandler(handler *vdom.EventHandler) {
	var id, exists = reg.getId[handler]
	if exists {
		delete(reg.handlers, id)
		delete(reg.getId, handler)
	}
}
func (reg *eventHandlerRegistry) getEventHandlerId(handler *vdom.EventHandler) (string, bool) {
	var id, exists = reg.getId[handler]
	if exists {
		return id, true
	} else {
		return "", false
	}
}


